import { type CrudConfig, type FormFieldConfig } from '@/components';
import DICT_CONSTANTS from '@/constants/dictConstants';
import urlConstants from '@/constants/urlConstants';
import { getRequestData } from '@/utils';
import type { ProColumns } from '@ant-design/pro-components';
import { Tag } from 'antd';

// 角色数据类型
export interface RoleRecord {
  roleId: string;
  roleName: string;
  roleStatus: string;
  mountStatus: string;
}

// 角色状态枚举工厂函数
export const getRoleStatusEnum = (translate: (prefix: string, key: string) => string) => [
  { key: 'Y', label: translate('systemManage', 'enable') },
  { key: 'N', label: translate('systemManage', 'unEnable') },
];

// 挂载状态枚举工厂函数
export const getMountStatusEnum = (translate: (prefix: string, key: string) => string) => [
  { key: 'Y', label: translate('systemManage', 'mounted') },
  { key: 'N', label: translate('systemManage', 'unmounted') },
];

// 统一的页面配置对象工厂函数
export const getPageConfig = (translate: (prefix: string, key: string) => string) => {
  const roleStatusEnum = getRoleStatusEnum(translate);
  const mountStatusEnum = getMountStatusEnum(translate);
  const t = (key: string) => translate('systemManage', key);
  return {
    // CRUD配置
    crudConfig: {
      endpoints: {
        list: urlConstants.ROLE_MANAGE.LIST,
        create: urlConstants.ROLE_MANAGE.CREATE,
        update: urlConstants.ROLE_MANAGE.EDIT,
        delete: urlConstants.ROLE_MANAGE.DELETE,
        detail: urlConstants.ROLE_MANAGE.LIST,
      },
      callbacks: {
        beforeCreate: async (data: any) => getRequestData(data, 'create', null),
        beforeUpdate: async (data: any) => getRequestData(data, 'edit', null),
        afterCreate: async (response: any) => console.log('角色创建成功:', response),
        afterUpdate: async (response: any) => console.log('角色更新成功:', response),
        afterDelete: async (response: any, id: string) => console.log('角色删除成功:', id),
        onError: (error: any, operation: string) => console.error(`角色操作 ${operation} 失败:`, error),
      },
    } as CrudConfig,

    // 搜索字段配置
    searchFields: [
      {
        name: 'roleName',
        label: t('roleName'),
        type: 'input' as const,
      },
      {
        name: 'roleStatus',
        label: t('roleStatus'),
        type: 'select' as const,
        options: roleStatusEnum.map((item) => ({
          label: item.label,
          value: item.key,
        })),
      },
      {
        name: 'mountStatus',
        label: t('mountStatus'),
        type: 'select' as const,
        options: mountStatusEnum.map((item) => ({
          label: item.label,
          value: item.key,
        })),
      },
    ],

    // 表格列配置
    columns: [
      {
        title: t('serialNumber'),
        dataIndex: 'paramIndex',
        key: 'paramIndex',
        width: 80,
        fixed: 'left',
      },
      {
        title: t('roleId'),
        dataIndex: 'roleId',
        key: 'roleId',
        width: 100,
        fixed: 'left',
      },
      {
        title: t('roleName'),
        dataIndex: 'roleName',
        key: 'roleName',
        width: 200,
        ellipsis: true,
        render: (text: string) => <span style={{ fontWeight: 500, color: '#1890ff' }}>{text}</span>,
      },
      {
        title: t('roleStatus'),
        dataIndex: 'roleStatus',
        key: 'roleStatus',
        width: 100,
        render: (text: string) => {
          const paramStatusMap: Record<string, any> = DICT_CONSTANTS.PARAM_STATUS || {};
          const statusMap = {
            Y: { color: 'green', text: t('enable') },
            N: { color: 'red', text: t('unEnable') },
          };
          const status = statusMap[text as keyof typeof statusMap] || {
            color: 'default',
            text: paramStatusMap[text] || text,
          };
          return <Tag color={status.color}>{status.text}</Tag>;
        },
      },
      {
        title: t('mountStatus'),
        dataIndex: 'mountStatus',
        key: 'mountStatus',
        width: 100,
        render: (text: string) => {
          const paramStatusMap: Record<string, any> = DICT_CONSTANTS.PARAM_STATUS || {};
          const mountMap = {
            Y: { color: 'blue', text: t('mounted') },
            N: { color: 'orange', text: t('unmounted') },
          };
          const status = mountMap[text as keyof typeof mountMap] || {
            color: 'default',
            text: paramStatusMap[text] || text,
          };
          return <Tag color={status.color}>{status.text}</Tag>;
        },
      },
    ] as ProColumns<RoleRecord>[],

    // 表单字段配置
    formFields: [
      {
        name: 'roleName',
        label: t('roleName'),
        type: 'input' as const,
        rules: [{ required: true, message: t('roleNamePlaceholder') }],
        props: {
          disabled: true,
        },
      },
      {
        name: 'roleStatus',
        label: t('roleStatus'),
        type: 'select' as const,
        options: roleStatusEnum.map((item) => ({
          label: item.label,
          value: item.key,
        })),
        rules: [{ required: true, message: t('roleStatusPlaceholder') }],
      },
      {
        name: 'mountStatus',
        label: t('mountStatus'),
        type: 'select' as const,
        options: mountStatusEnum.map((item) => ({
          label: item.label,
          value: item.key,
        })),
        rules: [{ required: true, message: t('mountStatusPlaceholder') }],
      },
    ] as FormFieldConfig[],

    // 用户管理相关配置（用于角色详情中的用户列表）
    userConfig: {
      // 搜索字段配置
      searchFields: [
        {
          name: 'userId',
          label: t('userId'),
          type: 'input' as const,
        },
        {
          name: 'userName',
          label: t('userName'),
          type: 'input' as const,
        },
      ],

      // 表格列配置
      columns: [
        {
          title: t('userId'),
          dataIndex: 'userId',
          key: 'userId',
          width: 120,
        },
        {
          title: t('userName'),
          dataIndex: 'userName',
          key: 'userName',
          width: 220,
          ellipsis: true,
          render: (text: string) => <div style={{ wordWrap: 'break-word', whiteSpace: 'normal' }}>{text}</div>,
        },
        {
          title: t('orgNo'),
          dataIndex: 'orgNo',
          key: 'orgNo',
          width: 120,
          render: (text: string) => {
            const labelTypeMap: Record<string, any> = DICT_CONSTANTS.LABEL_TYPE || {};
            return <Tag>{labelTypeMap[text] || text}</Tag>;
          },
        },
        {
          title: t('role'),
          dataIndex: 'role',
          key: 'role',
          width: 120,
          render: (text: string) => {
            const labelSourceMap: Record<string, any> = DICT_CONSTANTS.LABEL_SOURCE || {};
            return <Tag>{labelSourceMap[text] || text}</Tag>;
          },
        },
        {
          title: t('position'),
          dataIndex: 'position',
          key: 'position',
          width: 150,
          render: (text: string) => {
            const labelAttributeMap: Record<string, any> = DICT_CONSTANTS.LABEL_ATTRIBUTE || {};
            return <Tag>{labelAttributeMap[text] || text}</Tag>;
          },
        },
        {
          title: t('gender'),
          dataIndex: 'gender',
          key: 'gender',
          width: 100,
          render: (text: string) => {
            const paramStatusMap: Record<string, any> = DICT_CONSTANTS.PARAM_STATUS || {};
            return <Tag>{paramStatusMap[text] || text}</Tag>;
          },
        },
        {
          title: t('language'),
          dataIndex: 'language',
          key: 'language',
          width: 100,
          render: (text: string) => {
            const paramStatusMap: Record<string, any> = DICT_CONSTANTS.PARAM_STATUS || {};
            return <Tag>{paramStatusMap[text] || text}</Tag>;
          },
        },
        {
          title: t('level'),
          dataIndex: 'level',
          key: 'level',
          width: 100,
          render: (text: string) => {
            const paramStatusMap: Record<string, any> = DICT_CONSTANTS.PARAM_STATUS || {};
            return <Tag>{paramStatusMap[text] || text}</Tag>;
          },
        },
      ] as ProColumns<any>[],

      // API配置
      apiConfig: {
        endpoints: {
          list: urlConstants.USER_MANAGEMENT.LIST,
        },
        methods: {
          list: 'GET',
        },
      },
    },
  };
};
